package controller

import (
	"ez/apps/erp/auto/controller"
	erp "ez/apps/erp/document"
	"ez/config/code"
	"ez/custom/cc"
	"gitee.com/dreamwood/ez-go/ez"
	"time"
)

type BuyOrderPaymentController struct {
	controller.BuyOrderPaymentAutoController
}

func init() {
	c := &BuyOrderPaymentController{}
	c.SetRouteParam("/erp", "/admin", "BuyOrderPayment")
	ez.CreateApi(c.AdminGet(), c.GetAction).SetRouterName("[erp]采购订单付款_创建")
	ez.CreateApi(c.AdminList(), c.ListAction).SetRouterName("[erp]采购订单付款_列表")
	ez.CreateApi(c.AdminSave(), c.SaveAction).SetRouterName("[erp]采购订单付款_保存").Add(25, "lock", c.CheckLockAction)
	ez.CreateApi(c.AdminCopy(), c.CopyAction).SetRouterName("[erp]采购订单付款_复制")
	ez.CreateApi(c.AdminUpdate(), c.UpdateAction).SetRouterName("[erp]采购订单付款_更新")
	ez.CreateApi(c.AdminChoice(), c.ChoiceAction).SetRouterName("[erp]采购订单付款_选项")
	ez.CreateApi(c.AdminDelete(), c.DeleteAction).SetRouterName("[erp]采购订单付款_删除")
	ez.CreateApi(c.AdminUnDelete(), c.UnDeleteAction).SetRouterName("[erp]采购订单付款_恢复")
	ez.CreateApi(c.AdminDestroy(), c.DestroyAction).SetRouterName("[erp]采购订单付款_销毁")
	ez.CreateApi(c.AdminUpdateMany(), c.UpdateManyAction).SetRouterName("[erp]采购订单付款_更新_批量")
	ez.CreateApi(c.AdminDeleteMany(), c.DeleteManyAction).SetRouterName("[erp]采购订单付款_删除_批量")
	ez.CreateApi(c.AdminDestroyMany(), c.DestroyManyAction).SetRouterName("[erp]采购订单付款_销毁_批量")
	ez.CreateApi(c.AdminApi("/BuyOrderPayment/confirm"), c.SetPayedAction).
		SetRouterName("[erp]采购订单付款_确认付款").Add(25, "lock", c.CheckLockAction)
	ez.CreateApi(c.AdminApi("/BuyOrderPayment/unlock"), c.UnlockAction).SetRouterName("[erp]采购订单付款_解锁")
}

// func (c BuyOrderPaymentController) AccessControl(session *ez.Session) { /* 在这里面重构 */ }
// func (c BuyOrderPaymentController) GetAction(session *ez.Session) { /* 在这里面重构 */ }
// func (c BuyOrderPaymentController) ListAction(session *ez.Session) { /* 在这里面重构 */ }
func (c BuyOrderPaymentController) UnlockAction(session *ez.Session) {
	this := cc.New(session)
	buyOrderPayment, e := erp.NewBuyOrderPaymentCrud(session).FindId(this.Get("id").IsInt64())
	if this.Try(e) {
		return
	}
	buyOrderPayment.IsPay = false
	buyOrderPayment.IsLock = false
	_ = buyOrderPayment.Save()
	this.ReturnSuccess("解锁成功", "")
}
func (c BuyOrderPaymentController) SetPayedAction(session *ez.Session) {
	this := cc.New(session)
	buyOrderPayment, e := erp.NewBuyOrderPaymentCrud(session).FindId(this.Get("id").IsInt64())
	if this.Try(e) {
		return
	}
	if buyOrderPayment.IsPay {
		this.ReturnError(code.Error, "该记录已确认，请勿重复操作", "")
		return
	}
	buyOrderPayment.IsPay = true
	buyOrderPayment.PayAt = time.Now()
	buyOrderPayment.PayEmpId = this.GetUserId()
	buyOrderPayment.Save()
	this.ReturnSuccess("确认付款成功", "")
}

func (c BuyOrderPaymentController) CheckLockAction(session *ez.Session) {
	this := cc.New(session)
	model, e := erp.NewBuyOrderPaymentCrud(session).FindId(this.Get("id").IsInt64())
	if this.Try(e) {
		this.StopHandle()
		return
	}
	if model.IsLock {
		this.ReturnError(code.Error, "该记录已锁定，请联系管理员解锁", "")
		this.StopHandle()
		return
	}
}
